<template>
  <div>
    <el-dialog class="add_dialog" width="600px" :title="dialogTitle" :visible.sync="dialogVisible" :close-on-click-modal="false">
      <el-form ref="form" :rules="rules" :model="form" label-width="150px">
        <el-form-item label="上级菜单:">
          <treeselect v-model="form.pid" :options="menuList" placeholder="请选择上级菜单" search-nested />
        </el-form-item>
        <el-form-item label="菜单名称:" prop="title">
          <el-input v-model="form.title" placeholder="请输入菜单名称" />
        </el-form-item>
        <el-form-item label="控制器:">
          <el-input v-model="form.controller" placeholder="请输入控制器" />
        </el-form-item>
        <el-form-item label="方法:">
          <el-input v-model="form.action" type="textarea" placeholder="请输入方法" :autosize="{ minRows: 2, maxRows: 4 }" />
        </el-form-item>
        <el-form-item label="路径名称:">
          <el-input v-model="form.path" placeholder="请输入路径名称" />
        </el-form-item>
        <el-form-item label="状态:" prop="status">
          <el-radio v-model="form.status" :label="1">启用</el-radio>
          <el-radio v-model="form.status" :label="0">禁用</el-radio>
        </el-form-item>
        <el-form-item label="所属平台:" prop="type">
          <el-radio v-model="form.type" :label="1">管理后台</el-radio>
          <el-radio v-model="form.type" :label="2">供应商后台</el-radio>
        </el-form-item>
        <el-form-item label="排序:">
          <el-input-number v-model="form.sort_num" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="dialogVisible = false">取 消</el-button>
        <el-button size="small" type="primary" @click="submit">保 存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import Treeselect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
import { addMenu, editMenu } from '@/api/system'
export default {
  // props: ['editItem', 'menuList'],
  components: { Treeselect },
  props: {
    editItem: {
      type: Object,
      default: () => ({})
    },
    menuList: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      dialogVisible: false,
      form: {
        pid: null,
        title: '',
        controller: '',
        action: '',
        path: '',
        status: 1,
        type: 1,
        sort_num: 0
      },
      rules: {
        title: [{ required: true, message: '菜单名称不能为空', trigger: 'blur' }],
        status: [{ required: true, message: '请选择是否启用', trigger: 'change' }],
        type: [{ required: true, message: '请选择所属平台', trigger: 'change' }]
      }
    }
  },
  computed: {
    dialogTitle() {
      return this.editItem ? '编辑菜单' : '添加子菜单'
    }
  },
  watch: {
    dialogVisible(val) {
      if (val) {
        this.$nextTick(() => {
          this.$refs['form'].resetFields()
          if (this.editItem) {
            // 编辑
            for (const key in this.form) {
              if (key === 'pid') {
                this.form.pid = this.editItem.pid || null
              } else {
                this.form[key] = this.editItem[key]
              }
            }
            this.form.id = this.editItem.id
          }
        })
      } else {
        this.form = {
          pid: null,
          title: '',
          controller: '',
          action: '',
          path: '',
          status: 1,
          type: 1,
          sort_num: 0
        }
      }
    }
  },
  methods: {
    submit() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          const params = { ...this.form }
          params.pid = params.pid || 0
          if (this.editItem) {
            editMenu(params).then(res => {
              this.dialogVisible = false
              this.$emit('getList')
            })
          } else {
            addMenu(params).then(res => {
              this.dialogVisible = false
              this.$emit('getList')
            })
          }
        } else {
          this.$message({
            message: '请检查表单错误',
            type: 'error'
          })
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
